﻿
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using OnlineVideos.View_Models;
using OnlineVideos.Common;
using OnlineVideos.Data;
using OnlineVideos;
using Common.Library;
using System.Windows.Navigation;
using System.ComponentModel;
using OnlineVideos.UI;
using Microsoft.Phone.BackgroundAudio;
using System.Threading;
using Microsoft.Phone.Tasks;
using OnlineVideos.Views;

using OnlineVideos.Utilities;
using Common.Utilities;
using Microsoft.Phone.Shell;
using PicasaMobileInterface;
using OnlineMovies.UserControls;
using OnlineVideos.Entities;
using System.Threading.Tasks;

namespace OnlineVideos
{
    public partial class SubjectDetail : PhoneApplicationPage
    {
        public static SubjectDetail current = default(SubjectDetail);
        string navigationvalue = string.Empty;
        string pivotindex = string.Empty;
      public  PivotItem currentItem = default(PivotItem);
        public AddQuizPopup quizpop = default(AddQuizPopup);
        bool appbarvisible = true;
        public SubjectDetail()
        {
            InitializeComponent();
            current = this;
            quizpop = new AddQuizPopup();
            if (AppSettings.ShowID != "0")
            {
                LoadPivotThemes(AppSettings.ShowUniqueID);
            }
        }
        protected override void OnBackKeyPress(CancelEventArgs e)
        {
            if (this.LayoutRoot.Children.Contains(quizpop))
                this.LayoutRoot.Children.Remove(quizpop);
        }
        private void LoadPivotThemes(long ShowID)
        {
            pvtitmTest.Header = AppResources.ShowQuizPivotTitle;
            pvtMainDetails.Background = ShowDetail.LoadShowPivotBackground(ShowID);
            tblkVideosTitle.Text = OnlineShow.GetShowDetail(AppSettings.ShowUniqueID).Title;
        }

        private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
        {
            int showid = AppSettings.ShowUniqueID;
            string sharedstatus = Task.Run(async () => await Constants.connection.Table<ShowList>().Where(i => i.ShowID == showid).FirstOrDefaultAsync()).Result.ShareStatus;
            if (sharedstatus == "Shared To Blog")
                ((ApplicationBarMenuItem)ApplicationBar.MenuItems[0]).Text = "share this" + " " + AppResources.Type + "(" + "Shared)";
            else
                ((ApplicationBarMenuItem)ApplicationBar.MenuItems[0]).Text = "share this" + " " + AppResources.Type + "(" + "Not Shared)";
            PageHelper.RemoveEntryFromBackStack("SubjectDetail");
            string id = "";
            string QuizPivot = "";
            if (NavigationContext.QueryString.TryGetValue("id", out id))
                AppSettings.ShowID = id;
            if (NavigationContext.QueryString.TryGetValue("QuizPivot", out QuizPivot))
            {
                pvtMainDetails.SelectedIndex = 1;
                SettingsHelper.Save("QuizTitle", QuizPivot);
            }
        }
        private void imgTitle_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            NavigationService.Navigate(NavigationHelper.MainPanoramaPage);
        }
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
           
            int showid = AppSettings.ShowUniqueID;
            if (Constants.connection.Table<ShowList>().Where(i => i.ShowID == showid).FirstOrDefaultAsync().Result.Status != "Custom")
            {
                this.ApplicationBar.IsVisible = false;
                appbarvisible = false;
            }
            else
            {
                this.ApplicationBar.IsVisible = true;
            }
            if (NavigationContext.QueryString.TryGetValue("navigationvalue", out navigationvalue))
            {

                if (navigationvalue == "1")
                {
                    if (NavigationContext.QueryString.TryGetValue("pivotindex", out pivotindex))
                        pvtMainDetails.SelectedIndex = Convert.ToInt32(pivotindex);
                    Constants.navigationvalue++;
                }
                else
                {
                    if (NavigationContext.QueryString.TryGetValue("pivotindex", out pivotindex))
                        pvtMainDetails.SelectedIndex = Convert.ToInt32(pivotindex);
                    Constants.navigationvalue--;
                }
                while (NavigationService.BackStack.Count() > 1)
                {
                    if (!NavigationService.BackStack.FirstOrDefault().Source.OriginalString.Contains("SubjectDetail"))
                    {
                        NavigationService.RemoveBackEntry();
                    }
                    else
                    {
                        NavigationService.RemoveBackEntry();
                        break;
                    }
                }
            }
            if (VideosList.lbxSongsList.IsEnabled == false)
                VideosList.lbxSongsList.IsEnabled = true;
          
        }
        private void btnshare_Click_1(object sender, EventArgs e)
        {
            if (NetworkHelper.IsNetworkAvailableForDownloads())
            {
                UploadToBlog upload = new UploadToBlog(AppSettings.ShowUniqueID, page1: this);
            }

        }

        private void btnadd_Click_1(object sender, EventArgs e)
        {


            if ((sender as ApplicationBarMenuItem).Text.Contains("videos"))
            {
                Constants.DownloadTimer.Stop();
                Constants.Linkstype = "Songs";
                NavigationService.Navigate(new Uri("/Views/LinksFromOnline.xaml?language=" + string.Empty, UriKind.Relative));
            }
            if ((sender as ApplicationBarMenuItem).Text.Contains("quiz") || (sender as ApplicationBarMenuItem).Text.Contains("practice tests"))
            {
                Constants.DownloadTimer.Stop();
                this.LayoutRoot.Children.Add(quizpop);

            }
            else
            {
                Constants.DownloadTimer.Stop();
                NavigationService.Navigate(new Uri("/Views/AddQuiz.xaml", UriKind.Relative));
            }

        }

        private void pvtMainDetails_SelectionChanged_1(object sender, SelectionChangedEventArgs e)
        {
            currentItem = e.AddedItems[0] as PivotItem;
            if (appbarvisible == true)
            {
                if (currentItem != null)
                {
                    ((ApplicationBarMenuItem)ApplicationBar.MenuItems[1]).Text = "Add" + " " + currentItem.Header.ToString();

                }
                if (ShowQuiz.currentshowquiz.lbxSubjectsList.Visibility == Visibility.Visible && currentItem.Header.ToString() == "quiz")
                {
                    ((ApplicationBarMenuItem)ApplicationBar.MenuItems[1]).Text = "Add" + " " + currentItem.Header.ToString();
                }
                else if (ShowQuiz.currentshowquiz.lbxSubjectsList.Visibility == Visibility.Collapsed && currentItem.Header.ToString() == "quiz")
                {
                    ((ApplicationBarMenuItem)ApplicationBar.MenuItems[1]).Text = "Add" + " " + "questions";
                }
                else if (ShowQuiz.currentshowquiz.lbxSubjectsList.Visibility == Visibility.Visible && currentItem.Header.ToString() == "practice tests")
                {
                    ((ApplicationBarMenuItem)ApplicationBar.MenuItems[1]).Text = "Add" + " " + currentItem.Header.ToString();
                }
                else if (ShowQuiz.currentshowquiz.lbxSubjectsList.Visibility == Visibility.Collapsed && currentItem.Header.ToString() == "practice tests")
                {
                    ((ApplicationBarMenuItem)ApplicationBar.MenuItems[1]).Text = "Add" + " " + "questions";
                }

            }

        }


    }
}